import json

def print_all_uid():
    k = []
    v = []
    try:
        with open('userdata\\uid.json', 'r') as file:
            data = json.load(file)
            if not data:
                print("uid.json 文件为空")
            else:
                #print("当前的键值对:")
                for key, value in data.items():
                    #print(f"{key}:{value}")
                    k.append(key)
                    v.append(value)
        return k,v
    except FileNotFoundError:
        print("uid.json 文件不存在")

def add_uid(u):
    try:
        with open('userdata\\uid.json', 'r') as file:
            data = json.load(file)
    except Exception as err:
        #print(err)
        value = u
        #value = input("请输入新的 uid：")

        data = {'1': value}
        with open('userdata\\uid.json', 'w') as file:
            json.dump(data, file)
        return

    if not data:
        # 如果文件为空，创建一个初始的键值对数据
        with open('userdata\\uid.json', 'w') as file:
            json.dump(data, file)
        print("uid.json 文件为空，已创建初始的键值对数据")

    # 从这里开始继续添加新的键值对数据
    key = str(len(data) + 1)
    value = u
    #value = input("请输入新的 uid：")
    data[key] = value

    with open('userdata\\uid.json', 'w') as file:
        json.dump(data, file)

    print("已成功添加新UID")

def remove_uid(u):
    #print_all_uid()
    key = u
    #key = input("请输入要删除的键：")
    #print('不想删除输入0')

    try:
        with open('userdata\\uid.json', 'r') as file:
            data = json.load(file)
    except FileNotFoundError:
        print("uid.json 文件不存在")
        return

    if key in data:
        del data[key]
        # 调整其他键的值
        for i, k in enumerate(sorted(data.keys()), start=1):
            if k != str(i):
                data[str(i)] = data.pop(k)
        with open('userdata\\uid.json', 'w') as file:
            json.dump(data, file)
        print(f"已成功删除键为 {key} 的账号")
    elif key == 0:
        print('已取消删除')
    else:
        print(f"键 {key} 不存在")

def chose_uid():
    print_all_uid()
    key = int(input('输入对应uid键:'))
    with open('userdata\\uid.json', 'r') as file:
        data = json.load(file)
    vla = data[str(key)]
    print('当前选中的UID为:',vla)
    return vla

#add_uid()